import { config } from '@/utils/axios/config';
import axios,{
    AxiosError,
    AxiosInstance,
    AxiosRequestHeaders,
    AxiosResponse,
    InternalAxiosRequestConfig
  } from 'axios'
const { result_code, base_url, request_timeout } = config

const service: AxiosInstance = axios.create({
    baseURL: base_url,
    timeout: request_timeout,
    withCredentials: false 
})


// request拦截器
service.interceptors.request.use((config: InternalAxiosRequestConfig)=>{


    return config
},(error: AxiosError) => {
    Promise.reject(error)
})

service.interceptors.response.use((response: AxiosResponse<any>)=>{
    const { data } = response
    const config = response.config
   
    if (!data) {
      // 返回“[HTTP]请求没有返回值”;
      throw new Error()
    }
    
    return response.data
},(error: AxiosError) => {

    return Promise.reject(error)
})

export { service }